package org.phoenixframework.api.search.service.impl;

import lombok.AllArgsConstructor;
import org.phoenixframework.api.search.repository.MemberReadHistoryRepository;
import org.phoenixframework.api.search.service.IMemberReadHistoryService;
import org.phoenixframework.common.entity.ums.MemberReadHistory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author doublelifeke
 * Email: hautxxxyzjk@163.com
 * DateTime: 2021/7/25 13:28
 * Description: 会员浏览记录管理Service实现类
 */
@AllArgsConstructor
@Service
public class MemberReadHistoryServiceImpl implements IMemberReadHistoryService {

    private final MemberReadHistoryRepository memberReadHistoryRepository;

    /**
     * 生成浏览记录
     *
     * @param memberReadHistory
     */
    @Override
    public int create(MemberReadHistory memberReadHistory) {
        memberReadHistory.setMemberId(1L);
        double random = Math.random();
        memberReadHistory.setMemberNickname("nickname" + random);
        memberReadHistory.setMemberIcon("icon" + random);
        memberReadHistory.setId(1L);
        memberReadHistory.setCreateTime(new Date());
        memberReadHistoryRepository.save(memberReadHistory);
        return 1;
    }

    /**
     * 批量删除浏览记录
     *
     * @param ids
     */
    @Override
    public int delete(List<String> ids) {
        return 0;
    }

    /**
     * 分页获取用户浏览历史记录
     *
     * @param pageRequest
     */
    @Override
    public Page<MemberReadHistory> list(PageRequest pageRequest) {
        return memberReadHistoryRepository.findByMemberIdOrderByCreateTimeDesc(1L, pageRequest);
    }

    /**
     * 清空浏览记录
     */
    @Override
    public void clear() {
        memberReadHistoryRepository.deleteAllByMemberId(1L);
    }
}
